#include <ctime>
#include <cstdlib>
#include <QApplication>
#include "Wrappers/UnixUtils/Process.hpp"
#include "Wrappers/Pthread/Thread.hpp"
#include "PizzaOrderParser.hpp"
#include "Reception.hpp"

namespace pizzeria {

Reception::Reception() {
  srand(time(NULL));
}

Reception::~Reception() {
  std::vector<communication::WalkieTalkie *>::iterator it;

  for (it = mWalkieList.begin(); it != mWalkieList.end(); ++it) {
    delete *it;
  }
}

void Reception::TakeNewOrder(const std::string &order) {
  std::list<Pizza> list = PizzaOrderParser::GetPizzaListFromOrder(order);
  std::list<Pizza>::iterator it;

  for (it = list.begin(); it != list.end(); ++it) {
    std::cout << "new\n";
    communication::WalkieTalkie *kitchenWalkieTalkie =
	new communication::WalkieTalkie;
    mWalkieList.push_back(new communication::WalkieTalkie);
    mWalkieList[0]->LinkWith(*kitchenWalkieTalkie);

    wrappers::pthread::Thread kitchenThread;
    kitchenThread.SetCallableArgument(kitchenWalkieTalkie);
    kitchenThread.SetCallable(Kitchen::CreateKitchen);
    kitchenThread.Run();
    communication::Message message(communication::Message::NEW_ORDER, &(*it));
    mWalkieList[0]->Send(message);
  }
}

} /* !namespace pizzeria */
